﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace AF_Tools
{
    public static class AFDESHelper
    {
        /// <summary>
        /// 字符串转Char
        /// </summary>
        /// <param name="sourceString"></param>
        /// <returns></returns>
        public static string StringToChar(string sourceString)
        {
            if (string.IsNullOrEmpty(sourceString))
                return string.Empty;
            byte[] arrByte = Encoding.Unicode.GetBytes(sourceString);

            StringBuilder strBuilder = new StringBuilder();
            foreach (byte b in arrByte)
                strBuilder.Append(b.ToString() + "-");
            return strBuilder.ToString().TrimEnd('-');
        }
        /// <summary>
        /// Char转字符串
        /// </summary>
        /// <param name="encryptedString"></param>
        /// <returns></returns>
        public static string CharToString(string encryptedString)
        {
            if (string.IsNullOrEmpty(encryptedString))
                return string.Empty;

            string[] arrStr = encryptedString.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);

            byte[] arrByte = new byte[arrStr.Length];

            int index = 0;
            foreach (string s in arrStr)
            {
                arrByte[index] = byte.Parse(s);
                index++;
            }

            return Encoding.Unicode.GetString(arrByte);
        }
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="sourceString"></param>
        /// <param name="key">8位字符</param>
        /// <param name="iv">8位字符</param>
        /// <returns></returns>
        public static string Encrypt(string sourceString, string key, string iv)
        {
            try
            {
                byte[] btKey = Encoding.UTF8.GetBytes(key);
                byte[] btIV = Encoding.UTF8.GetBytes(iv);
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] inData = Encoding.UTF8.GetBytes(sourceString);
                    try
                    {
                        using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
                        {
                            cs.Write(inData, 0, inData.Length);
                            cs.FlushFinalBlock();
                        }
                        return Convert.ToBase64String(ms.ToArray());
                    }
                    catch
                    {
                        return sourceString;
                    }
                }
            }
            catch { }
            return "DES加密出错";
        }


        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="encryptedString"></param>
        /// <param name="key">8位字符</param>
        /// <param name="iv">8位字符</param>
        /// <returns></returns>
        public static string Decrypt(string encryptedString, string key, string iv)
        {
            byte[] btKey = Encoding.UTF8.GetBytes(key);
            byte[] btIV = Encoding.UTF8.GetBytes(iv);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            using (MemoryStream ms = new MemoryStream())
            {
                byte[] inData = Convert.FromBase64String(encryptedString);
                try
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
                    {
                        cs.Write(inData, 0, inData.Length);
                        cs.FlushFinalBlock();
                    }
                    return Encoding.UTF8.GetString(ms.ToArray());
                }
                catch
                {
                    return encryptedString;
                }
            }
        }
    }
}